package com.zut.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zut.mapper.ParentsMapper;
import com.zut.pojo.Parents;
import com.zut.service.ParentsService;
import com.zut.service.impl.ParentsServiceImpl;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author parents
 * @since 2021-05-31
 */
@Controller

public class ParentsController {
    @Resource
    private ParentsServiceImpl parentsService;

    @Resource
    private ParentsMapper parentsMapper;

    //修改家长信息
    @RequestMapping("/updateParents")
    public void updateParents(String parname, String parpwd, String stuname, String stusex, int stuage, int parhour, String parphone, Model model){
        parentsService.updateParents(parname,parpwd,stuname,stusex,stuage,parhour,parphone);
        model.addAttribute("msg","修改成功");
    }



    //按用户名查询家长信息
    @RequestMapping("/selectParentsByName")
    public void selectParentsByName(String parname,Model model){
        Parents parents = parentsService.selectParentsByName(parname);
        model.addAttribute(parents);
    }

    //按id查询家长信息
    @RequestMapping("/admpardetail/{id}")
    public void selectParentsById(@PathVariable("id")Integer id,Model model){
        Parents parents = parentsService.selectParentsById(id);
        model.addAttribute(parents);
    }

    //家长绑定孩子信息
    @PostMapping("/addChildMsg")
    public String addChildMsg(HttpSession session,String cname,String sex,Integer cage){
        Parents parent = (Parents) session.getAttribute("loginParent");

        Parents parents = parentsService.selectParentsById(parent.getParid());
        parents.setStuname(cname);
        parents.setStusex(sex);
        parents.setStuage(cage);

        int i = parentsMapper.updateById(parents);
        session.removeAttribute("loginParent");
        return "log-in";

    }

    //修改已登录用户的信息
    @PostMapping("/modify")
    public String modify(HttpSession session,String password,Model model){
        Parents parent = (Parents) session.getAttribute("loginParent");

        Parents parents = parentsService.selectParentsById(parent.getParid());
        parents.setParpwd(password);
        model.addAttribute("msg","密码修改成功,请重新登录");
        parentsMapper.updateById(parents);
        session.removeAttribute("loginParent");
        return "log-in";
    }


}

